package com.codingapi.core.framework.utils;

import com.codingapi.netflix.framework.exception.ServerFeignException;
import com.lorne.core.framework.utils.DateUtil;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;

/**
 * @author modificial
 * @date 2018/4/10
 * @company codingApi
 * @description 时间工具类
 */
public class DateUtils {
    /**
     * 一般格式
     */
    public static final String CHINA_STYLE = "yyyy-MM-dd HH:mm:ss";

    private DateUtils() {
        // 防止初始化
    }

    /**
     * 获取当前时间
     *
     * @return 当前系统时间
     */
    public static final Date now() {
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDate localDate = LocalDate.now();
        ZonedDateTime zdt = localDate.atStartOfDay(zoneId);
        Date date = Date.from(zdt.toInstant());
        return date;
    }

    /**
     * 时间转字符串
     *
     * @param date    时间
     * @param pattern 转换形式
     * @return
     */
    public static final String format(Date date, String pattern) {
        return DateFormatUtils.format(date, pattern);
    }

    /**
     * 获取当前日期
     *
     * @return
     */
    public static final String nowDate() throws ServerFeignException {
        return DateUtil.formatDate(new Date(), DateUtil.DATE_FORMAT);
    }

    /**
     * 获取当前时间
     *
     * @return
     */
    public static final Date newDate() {
        return new Date();
    }


    /**
     * 获取某个日期的前一天
     */
    public static Date dayDefore(Date date) {
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        ca.add(Calendar.DATE, -1);
        Date lastMonth = ca.getTime();
        return lastMonth;
    }

}
